<template>
    <div class="df fdc jcc w100 h100 p40 bsbb aife">
        <div class="df aic w100 header">
            <img class="logo" src="@/assets/imgs/logo.png" alt="">
            <img class="logo2" src="@/assets/imgs/lname.png" alt="">
            <img class="logo3" src="@/assets/imgs/lename.png" alt="">
        </div>
        <div class="f1 w100 user-main df aic jcc">
            <div style="width: 740px;height: 350px;" class="mian">
                <el-scrollbar ref="scrollContainer" height="350px" style="padding: 20px;margin-bottom: 80px;width: 740px;">
                    
                        <div style="box-sizing: border-box;" ref="innerContainer">
                            <div class="df aic">
                                <p class="mian-title mr20">添加收藏</p>
                                <el-switch v-model="keepData" class="ml-2" inline-prompt
                                    style="--el-switch-on-color: #00BB88; --el-switch-off-color: #7C8599" active-text="保留数据"
                                    inactive-text="保留数据" />
                            </div>
                            <p style="margin-top: 4px;" class="tal">粘贴或输入 URL链接，极速体验 AI内容解读</p>
                            <input @blur="confirmUrl" @focus="successUrl = 'url-ipt'" v-show="!show" class="ipt mt20"
                                spellcheck="false" :class="successUrl" v-model="useUrl" placeholder="请粘贴或输入URL" />
                            <div v-show="show" style="border: 1px solid #DEE0E3;border-radius: 8px;"
                                class="df aic w100 p10 bsbb mt20">
                                <img style="width: 34px;height: 34px;" :src="activeImgUrl" alt="">
                                <div class="ml20">
                                    <div style="color: #0C0D0E;">内容读取中···</div>
                                    <div style="margin-top: 2px;">{{ useUrl }}</div>
                                </div>
                            </div>
                            <div v-show="!show">
                                <p class="tal mt20  c7">支持以下内容平台和大部分网站</p>
                                <div class="df aic mt20">
                                    <div>
                                            <img v-show="activeImg == 1" class="img-item" src="@/assets/imgs/xhsm.png" alt="">
                                            <img v-show="activeImg != 1" class="img-item" src="@/assets/imgs/hxhs.png" alt="">
                                        </div>
                                        <div>
                                            <img v-show="activeImg == 2" class="img-item" src="@/assets/imgs/wxm.png" alt="">
                                            <img v-show="activeImg != 2" class="img-item" src="@/assets/imgs/hwx.png" alt="">
                                        </div>
                                        <div>
                                            <img v-show="activeImg == 3" class="img-item" src="@/assets/imgs/dym.png" alt="">
                                            <img v-show="activeImg != 3" class="img-item" src="@/assets/imgs/hdy.png" alt="">
                                        </div>
                                        <div>
                                            <img v-show="activeImg == 4" class="img-item" src="@/assets/imgs/blm.png" alt="">
                                            <img v-show="activeImg != 4" class="img-item" src="@/assets/imgs/hbl.png" alt="">
                                        </div>
                                        <div>
                                            <img v-show="activeImg == 5" class="img-item" src="@/assets/imgs/ttm.png" alt="">
                                            <img v-show="activeImg != 5" class="img-item" src="@/assets/imgs/htt.png" alt="">
                                        </div>
                                        <div>
                                            <img v-show="activeImg == 6" class="img-item" src="@/assets/imgs/otherm.png" alt="">
                                            <img v-show="activeImg != 6" class="img-item" src="@/assets/imgs/hother.png" alt="">
                                        </div>
                                    <!-- <img class="img-item" src="@/assets/imgs/xhsm.png" alt="">
                                    <img class="img-item" src="@/assets/imgs/wxm.png" alt="">
                                    <img class="img-item" src="@/assets/imgs/dym.png" alt="">
                                    <img class="img-item" src="@/assets/imgs/blm.png" alt="">
                                    <img class="img-item" src="@/assets/imgs/ttm.png" alt="">
                                    <img class="img-item" src="@/assets/imgs/otherm.png" alt=""> -->
                                </div>
                            </div>
                            <div style="margin-bottom: 80px;" v-show="show">
                                <div class="mt20">
                                    <div class="df aic ai-title ">
                                        <p class="mr10">AI解读设置</p>
                                        <p class="choose-item df aic jcc">{{ aiOption.length }}</p>
                                    </div>
                                    <el-checkbox-group class="mt20" text-color="#00BB88" v-model="aiOption" size="small">
                                        <el-checkbox label="Option1" value="quick_summary" border>
                                            <template #default>
                                                <div style="margin-left: 12px;" class="df fdc">
                                                    <div class="fs14 fw600 mb10 c00">速读摘要</div>
                                                    <div class="c7c">客观摘要，保留原文结构</div>
                                                </div>
                                            </template>
                                        </el-checkbox>
                                        <el-checkbox @change="change2" label="Option2" value="personalized" border>
                                            <template #default>
                                                <div style="margin-left: 12px;" class="df fdc">
                                                    <div class="fs14 fw600 mb10 c00">专业解读</div>
                                                    <div class="c7c">与您职业相关的专业角度解读 </div>
                                                </div>
                                            </template>
                                        </el-checkbox>
                                        <el-checkbox @change="change" label="Option2" value="custom_prompt" border>
                                            <template #default>
                                                <div style="margin-left: 12px;" class="df fdc">
                                                    <div class="fs14 fw600 mb10 c00">自定义解读</div>
                                                    <div class="c7c">自定义角度让喵灵解读</div>
                                                </div>
                                            </template>
                                        </el-checkbox>
                                    </el-checkbox-group>
                                </div>
                                <div class="mt20">
                                    <div class="p20 bsbb bcf8 br8 ">
                                        <div style="font-weight: 600;color: #42464E;margin-bottom: 10px;">你的职业信息</div>
                                        <el-form :model="form" label-position="left" label-width="100">
                                            <el-form-item label="行业/学生">
                                                <el-input v-model="form.type" placeholder="请输入"></el-input>
                                            </el-form-item>
                                            <el-form-item label="职业/专业">
                                                <el-input v-model="form.item" placeholder="请输入"></el-input>
                                            </el-form-item>
                                            <el-form-item label="经验">
                                                <el-select v-model="form.expirence">
                                                    <el-option v-for="item in expirences" :key="item" :label="item.label"
                                                        :value="item.value"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-form>
                                    </div>
                                    <div v-show="aiOption.includes('custom_prompt')" class="p20 bsbb bcf8 mt20 br8 ">
                                        <p>自定义解读</p>
                                        <el-input v-model="perfessional" type="textarea" :rows="4"></el-input>
                                    </div>
                                </div>
                            </div>
                            <div class="df jcfe confirm p20">
                                <el-button style="background-color: #F6F8FA;border: 1px solid rgba(0,0,0,.1);color: #42464E;" @click="cancle" type="info">取消</el-button>
                                <el-button :disabled="!show" @click="goDetail" :loading="loading" class="bc0 cf">确认</el-button>
                            </div>
                        </div>
                </el-scrollbar>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { validate_url, add_url } from '@/apis/bookmarks';
import { ElMessage } from 'element-plus';

const router = useRouter()

const keepData = ref(true)
const useUrl = ref('')
const successUrl = ref('url-ipt')
const activeImg = ref(0)
const form = ref({
    type: '',
    item: '',
    expirence: ''
})
const loading = ref(false)
const expirences = ref([
    { label: "初级", value: "初级" },
    { label: "中级", value: "中级" },
    { label: "资深", value: "资深" },
])
const chooseItem = ref(0)
const aiOption = ref([])
const show = ref(false)
const activeImgUrl = ref('')
const loading1 = ref(false)
const perfessional = ref('')
const scrollContainer = ref()
const innerContainer = ref()


const cancle = () => {
    useUrl.value = ' '
    show.value = false
}
const goDetail = async () => {
    loading.value = true
    let data = {
        personalized_options: {
            industry: '',
            profession: '',
            experience: ''
        }
    }
    data.favorite_id = 0
    if (aiOption.value.includes('quick_summary')) {
        data.quick_summary = 1
    }
    if (aiOption.value.includes('personalized')) {
        data.personalized = 1
    }
    if (aiOption.value.includes('custom_prompt')) {
        data.custom_prompt = 1
    }


    data.custom_prompt_text = perfessional.value

    if (aiOption.value.includes('personalized')) {
        if (!form.value.type) {
            ElMessage.info('请填写您的行业')
            loading.value = false
            return
        }
        if (!form.value.item) {
            ElMessage.info('请填写您的专业')
            loading.value = false
            return
        }
        if (!form.value.expirence) {
            ElMessage.info('请填写您的从业经验')
            loading.value = false
            return
        }
    }
    if (aiOption.value.includes('custom_prompt')) {
        if (!perfessional.value) {
            ElMessage.info('请填写自定义解读内容')
            loading.value = false
            return
        }
    }
    data.personalized_options.industry = form.value.type
    data.personalized_options.profession = form.value.item
    data.personalized_options.experience = form.value.expirence
    data.url = useUrl.value
    try {
        let add_res = await add_url(data)
        if (add_res.data.code == 200) {
            loading.value = false
            router.push('/view')
        } else {
            loading.value = false
        }
    } catch (e) {
        loading.value = false
    }
}

const change = (e) => {
    if (e) {
        scrollContainer.value.setScrollTop(innerContainer.value.clientHeight)
    }
}
const change2 = (e) => {
    if (e) {
        scrollContainer.value.setScrollTop(innerContainer.value.clientHeight)
    }
}
const checkUrl = async () => {
    let data = {}
    data.url = useUrl.value
    try {
        let vali_res = await validate_url(data)
        if (vali_res.data.data.is_accessible) {
            loading1.value = false
            show.value = true
            successUrl.value = 'url-ipt'
        } else {
            loading1.value = false
            successUrl.value = 'error-ipt'
        }
        switch (vali_res.data.data.site_type) {
            case 'xiaohongshu':
                activeImg.value = 1
                activeImgUrl.value = '/xhsm.png'
                break
            case 'weixin':
                activeImg.value = 2
                activeImgUrl.value = '/wxm.png'
                break
            case 'douyin':
                activeImg.value = 3
                activeImgUrl.value = '/dym.png'
                break
            case 'bilibili':
                activeImg.value = 4
                activeImgUrl.value = '/blm.png'
                break
            case 'toutiao':
                activeImg.value = 5
                activeImgUrl.value = '/ttm.png'
                break
            case 'other':
                activeImg.value = 6
                activeImgUrl.value = '/otherm.png'
                break
        }
    } catch (e) {
        console.log(e);
    }
}

const confirmUrl = async () => {
    loading1.value = true
    if (!useUrl.value) {
        ElMessage.error('输入或粘贴的url不能为空')
        loading1.value = false
        return
    }
    checkUrl()
}
</script>
<style lang="scss" scoped>
.logo {
    width: 3%;
    margin-right: 20px;
}

.logo2 {
    width: 5%;
    margin-right: 10px;
}

.logo3 {
    width: 11%;
}

.user-main {
    background-image: url('@/assets/imgs/banner.png');
    background-position: center;
    background-size: 100%;
    background-repeat: no-repeat;
    border-radius: 20px;
    margin-top: 10px;
}

.mian {
    background-color: #fff;
    position: relative;
    width: 50%;
    // padding: 20px;
    height: 400px;
    // overflow-y: scroll;
    box-sizing: border-box;
    box-shadow: 0 40px 10px -5px;
    border-radius: 20px;
    // padding-bottom: 60px;
    box-sizing: border-box;
}

.img-item {
    width: 32px;
    height: 32px;
    margin-right: 10px;
}

.c7 {
    color: #7C8599;
}

.mian-title {
    color: #0C0D0E;
    font-size: 20px;
    font-weight: 600;
}

.ipt {
    height: 40px;
    // width: 100%;
    width: fit-content;
    box-sizing: border-box;
}

.confirm {
    box-sizing: border-box;
    width: 100%;
    position: absolute;
    left: 0;
    bottom: 0;
    border-top: 1px solid #C9CDD44D;
    background-color: #fff;
    border-radius: 0 0 20px 20px;
    z-index: 999;
}

.url-ipt {
    box-sizing: border-box;
    width: 100%;
    outline: none;
    border: none;
    padding: 5px 10px;
    border: 1px solid #409EFF;
    border-radius: 4px;

}

.error-ipt {
    box-sizing: border-box;
    width: 100%;
    outline: none;
    border: none;
    padding: 5px 10px;
    border: 1px solid red;
    border-radius: 4px;
    color: red;
}

.choose-item {
    border-radius: 100%;
    background-color: #41464F;
    color: #fff;
    width: 18px;
    height: 18px;
    font-weight: 700;
    border: 1px solid #41464F;
}

:deep(.el-switch__core) {
    border-radius: 3px !important;
    padding: 0 5px;
}

:deep(.el-switch__action) {
    width: 5px;
    border-radius: 3px !important;
    left: 5px;
}

:deep(.el-checkbox-group) {
    height: 56px !important;
    display: flex;
    justify-content: space-between;
}

:deep(.el-checkbox) {
    height: 56px !important;
    width: 30%;
    border-radius: 8px !important;
    border-color: #00BB88 !important;
}

:deep(.el-checkbox__inner) {
    // background-color:  #00BB88 !important;
    border-color: #00BB88 !important;
}

// :deep(.el-checkbox > .is-checked){
//     background-color:  #00BB88 !important;
// }
:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
    background-color: #00BB88 !important;
    border-color: #00BB88 !important;
}
</style>